﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Drawing;
using System.Threading;

using XPTable;
using XPTable.Models;
using XPTable.Renderers;

namespace WinBird
{
    public partial class MainForm
    {
        //System.Windows.Forms.ListViewGroup groupMessages = new System.Windows.Forms.ListViewGroup("Сообщения", System.Windows.Forms.HorizontalAlignment.Center);
        //System.Windows.Forms.ListViewGroup groupOnline = new System.Windows.Forms.ListViewGroup("В сети", System.Windows.Forms.HorizontalAlignment.Center);
        //System.Windows.Forms.ListViewGroup groupOffline = new System.Windows.Forms.ListViewGroup("Не в сети", System.Windows.Forms.HorizontalAlignment.Center);

        Thread threadOnlineFriends;
        Thread threadFriends;

        private void Friends()
        {
            threadFriends = new Thread(ThreadFriends);
            threadOnlineFriends = new Thread(ThreadOnlineFriends);

            threadFriends.Priority = ThreadPriority.Lowest;
            threadOnlineFriends.Priority = ThreadPriority.Lowest;

            threadFriends.IsBackground = false;
            threadOnlineFriends.IsBackground = true;

            threadFriends.Start();
            threadOnlineFriends.Start();
        }

        private void getFriends(int uid = 0, bool clear = true)
        {
            if (clear == true)
            {
                this.listFriends.TableModel.Rows.Clear();
            }

            try
            {
                Array[] result = api.getApi("friends.get", "user",
                    new string[] { "uid", "first_name", "last_name", "photo_medium", "online" },
                    new string[,] { { "uid", uid.ToString() } }
                    );

                int i = 0;
                if (clear != true)
                {
                    i = this.listFriends.TableModel.Rows.Count;
                }
                foreach (string[] content in result)
                {
                    this.listFriends.TableModel.Rows.Add(new Row());
                    this.listFriends.TableModel.Rows[i].Tag = content[0];
                    this.listFriends.TableModel.Rows[i].Cells.Add(new Cell(""));
                    this.listFriends.TableModel.Rows[i].Cells.Add(new Cell(content[2] + " " + content[1]));
                    this.listFriends.TableModel.Rows[i].Cells.Add(new Cell(content[4]));

                    if (content[4] == "1")
                    {
                        this.listFriends.TableModel.Rows[i].Cells[2].Text = "1";
                        this.listFriends.TableModel.Rows[i].Cells[1].ForeColor = Color.LightGreen;
                    }
                    else
                    {
                        this.listFriends.TableModel.Rows[i].Cells[2].Text = "0";
                        this.listFriends.TableModel.Rows[i].Cells[1].ForeColor = Color.Silver;
                    }

                    if (api.UserID == int.Parse(content[0]))
                    {
                        this.listFriends.TableModel.Rows[i].Cells[0].Text = "4";
                    }

                    this.listFriends.TableModel.Rows[i].Cells[0].Font = new System.Drawing.Font("Webdings", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));

                    i++;
                }

                friends_titleColumn.Sortable = true;
                this.listFriends.Sort(1, SortOrder.Ascending, true);
                this.listFriends.Sort(2, SortOrder.Descending, true);
                friends_titleColumn.Sortable = false;

                foreach (XPTable.Models.Row r in this.listFriends.TableModel.Rows)
                {
                    if (r.Cells[0].Text == "4")
                    {
                        this.listFriends.EnsureVisible(r.Index, 0);
                    }
                }

                
            }
            catch (Exception e)
            {
                err.setError(e.Message);
            }
        }

        private void onlineFriends()
        {
            try
            {
                Array[] result = api.getApi("friends.getOnline", "uid",
                    new string[] { "uid" }
                    );

                foreach (XPTable.Models.Row r in this.listFriends.TableModel.Rows)
                {
                    if (r.Cells[2].Text != "0")
                    {
                        if (r.Cells[2].Text != "2")
                        {
                            r.Cells[2].Text = "0";
                            r.Cells[1].ForeColor = Color.Silver;
                        }
                    }
                }

                foreach (XPTable.Models.Row r in this.listFriends.TableModel.Rows)
                {
                    foreach (string[] content in result)
                    {
                        if (r.Tag.ToString() == content[0])
                        {
                            r.Cells[2].Text = "1";
                            r.Cells[1].ForeColor = Color.LightGreen;
                        }
                    }
                }

                friends_titleColumn.Sortable = true;
                this.listFriends.Sort(1, SortOrder.Ascending, true);
                this.listFriends.Sort(2, SortOrder.Descending, true);
                friends_titleColumn.Sortable = false;
            }
            catch (Exception)
            {
                err.setError("Не удалось обновить список друзей!");
            }
        }

        private void ThreadOnlineFriends()
        {
            while (true)
            {
                this.listFriends.Invoke(new delegateOnlineFriends(this.onlineFriends));
                Thread.Sleep(1 * 30 * 1000);
            }
        }
        public delegate void delegateOnlineFriends();

        private void ThreadFriends()
        {
            this.listFriends.Invoke(new delegateFriends(this.getFriends), new object[] { 0, true });
        }
        public delegate void delegateFriends(int uid = 0, bool clear = true);

        private void listFriends_CellClick(object sender, XPTable.Events.CellMouseEventArgs e)
        {
            try
            {
                this.listFriends.Cursor = System.Windows.Forms.Cursors.WaitCursor;

                int index = e.Row;
                api.UserID = int.Parse(this.listFriends.TableModel.Rows[index].Tag.ToString());

                foreach (XPTable.Models.Row r in this.listFriends.TableModel.Rows)
                {
                    r.Cells[0].Text = "";
                }

                this.listFriends.TableModel.Rows[index].Cells[0].Text = "4";

                this.getAudio(api.UserID);
            }
            catch (Exception)
            {
                err.setError("Не удалось загрузить список аудиозаписей");
            }
        }

        private void menuViewFriends_Click(object sender, EventArgs e)
        {
            try
            {
                int index = this.listFriends.TableModel.Table.SelectedItems[0].Index;

                int uid = int.Parse(this.listFriends.TableModel.Rows[index].Tag.ToString());

                string text = this.listFriends.TableModel.Rows[index].Cells[1].Text;

                this.listFriends.TableModel.Rows.Clear();

                this.listFriends.TableModel.Rows.Add(new Row());
                this.listFriends.TableModel.Rows[0].Tag = uid;
                this.listFriends.TableModel.Rows[0].Cells.Add(new Cell(""));
                this.listFriends.TableModel.Rows[0].Cells.Add(new Cell(text));
                this.listFriends.TableModel.Rows[0].Cells.Add(new Cell("2"));

                if (api.UserID == uid)
                {
                    this.listFriends.TableModel.Rows[0].Cells[0].Text = "4";
                }

                this.listFriends.TableModel.Rows[0].Cells[0].Font = new System.Drawing.Font("Webdings", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(2)));
                this.listFriends.TableModel.Rows[0].ForeColor = Color.LemonChiffon;

                this.getFriends(uid, false);
            }
            catch (Exception)
            {
                err.setError("Не удалось загрузить список друзей");
            }
        }

        private void listFriends_ClientSizeChanged(object sender, EventArgs e)
        {
            this.friends_titleColumn.Width = this.listFriends.Width
                - friends_idColumn.Width
                - friends_viewColumn.Width
                - 25;
        }


    }
}
